import {
  ServiceModel
} from '../../../../models/service'
const modelService = new ServiceModel()
const App =  getApp();
import { decodeUnicode } from '../../../../utils/index.js'


Page({

  /**
   * 页面的初始数据
   */
  data: {
    serviceInfo: {},
    returnId: '',
    cancel: false,
    return:false,
    statusBarHeight: App.globalData.statusBarHeight,
    isTabbar:false
  },
  
  //获取售后详情
  getServiceDetail: function (returnId) {
    const self = this;
    const params = {
      returnId: self.data.returnId
      // returnId:'ea20ae4796c54cdea4f38b80545a6b2e'
    }

    modelService.getServiceDetail(params).then(res => {
      if (res.data.RefundDetails.problemDes) {
        res.data.RefundDetails.problemDes = decodeUnicode(res.data.RefundDetails.problemDes)
        
      }
      if (res.data.RefundDetails.returnReason) {
        res.data.RefundDetails.returnReason = decodeUnicode(res.data.RefundDetails.returnReason)

      }
      self.setData({
        serviceInfo: res.data.RefundDetails
      })
      self.showCancelBtn();
      self.showReturnInfo();
      wx.stopPullDownRefresh();
    }).catch();
  },
    
  //是否显示撤销申请按钮
  showCancelBtn: function () {
    const self = this;
    const returnType = self.data.serviceInfo.returnType;
    const returnStatus = self.data.serviceInfo.returnStatus;
    if (returnType !== 1 && returnType !== 2) {
      if (returnStatus == 1) {
        self.setData({
          cancel: true
        })
      }
    }
  },
    
  //是否显示退货信息
  showReturnInfo: function () {
    const self = this;
    const returnType = self.data.serviceInfo.returnType;
    const returnStatus = self.data.serviceInfo.returnStatus;
    if (returnType == 4 || returnType == 5 || returnType == 6) {
      if (returnStatus == 3 || returnStatus == 4 || returnStatus == 5) {
        self.setData({
          return:true
        })
      }
    }
    },
    
    //撤销申请
    cancel: function () {
      const self = this;
      const params = {
        returnId:self.data.returnId
      }

      
        wx.showModal({
          title: '提示',
          content: '是否确认撤销申请',
          success: function (res) {
            if (res.confirm) {
              modelService.cancelRefund(params).then(res => {
                App.WxService.showToast({ title: '撤销成功', icon: 'none' });
                //跳转到订单列表
                wx.redirectTo({
                  url: '/pages/account/service/list/index'
                })
              }).catch();
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      
    },
  
    /**
     * 生命周期函数--监听页面加载
     */
  onLoad: function (options) {
      this.setData({
        returnId:options.returnId  
      })
      this.getServiceDetail();
    },
  
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
      
    },
  
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      
    },
  
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },
  
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
      // var pages = getCurrentPages() //  获取页面栈
      // var prevPage = pages[pages.length - 2]
      // if(prevPage.path == 'pages/account/order/customer-service/index'){
      //   wx.navigateBack({
      //     delta: 2
      //   })
      // }
    },
  
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
      this.getServiceDetail();
    },
  
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
      
    },
  
  })